<?php

namespace app\controller\merchant;

use app\BaseController;
use app\model\StationAccount;
use think\facade\Cache;
use think\response\Json;

class Account extends BaseController
{
    public function login(): Json
    {
        return $this->handle(function () {
            return $this->app->make(StationAccount::class)->login();
        });
    }

    public function detail(): Json
    {
        return $this->handle(function () {
            return $this->request->account;
        });
    }

    public function modify(): Json
    {
        return $this->handle(function () {
            $account = $this->request->account;
            $param = $this->request->post();
            if (!checkPassword($param['old_password'], $account['password'], $account['token'])) throw new \Exception("旧密码错误");
            $pwd = setPassword($param['new_password']);
            $account->password = $pwd['password'];
            $account->token = $pwd['token'];
            $account->save();
            return $account;
        });
    }
}
